﻿Imports BUS
Imports DAO

Public Class FormBaoCaoTon

    Private Sub FormBaoCaoTon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Dim tb As DataTable

#Region "Xử lý nút xem báo cáo"
    Private Sub ButtonShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim index = ComboBoxThang.SelectedIndex
        If index > -1 Then
            Dim thangbaocao As Integer = Integer.Parse(ComboBoxThang.SelectedItem.ToString())
            Dim thanghientai As Integer
            thanghientai = Date.Today.Month
            If thangbaocao <= thanghientai Then
                tb = Sach_BUS.BaoCao(thangbaocao)
                Dim rpt As New CrystalReportSach()
                rpt.SetDataSource(tb)
                rpt.SetParameterValue("thang", thangbaocao.ToString())
                CrystalReportViewSach.ReportSource = rpt
            Else
                MessageBox.Show("Tháng chọn báo cáo không hợp lệ")
            End If
        Else
            MessageBox.Show("Chưa chọn tháng")
        End If
    End Sub
#End Region

    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

#Region "Xử lý nút ghi báo cáo ra file exel"
    Private Sub ButtonGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGhi.Click
        Dim browser As New SaveFileDialog()
        browser.Filter = ".xls|*.xls"
        browser.ShowDialog()

        Dim src As String = browser.FileName

        If src <> "" Then
            Dim app As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
            Dim workbook As Microsoft.Office.Interop.Excel._Workbook = app.Workbooks.Add(Type.Missing)
            Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
            worksheet = workbook.Sheets("Sheet1")
            worksheet = workbook.ActiveSheet
            worksheet.Name = "Bao Cao Sach Ton"

            Dim i, j As Integer
            Dim col As Integer = 1
            For j = 0 To tb.Columns.Count - 1
                Dim r As Microsoft.Office.Interop.Excel.Range = worksheet.Cells(1, col)
                r.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue)
                r.Font.Bold = True
                worksheet.Cells(1, col) = tb.Columns(j).ColumnName
                col = col + 1
            Next

            For i = 0 To tb.Rows.Count - 2
                col = 1
                For j = 0 To tb.Columns.Count - 1
                    worksheet.Cells(i + 2, col) = tb.Rows(i).Item(j).ToString()
                    col = col + 1
                Next
            Next

            workbook.SaveAs(src, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
            MessageBox.Show("Đã ghi file thành công")
            app.Visible = False
            app.Quit()
        End If
    End Sub
#End Region
End Class